package com.net.xpay.core.manager.impl.collect;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.xpay.core.manager.collect.CollectionSafePayOrderManager;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.collect.CollectionSafePayOrderMapper;
import com.net.xpay.core.domain.collect.CollectionSafePayOrder;
import com.net.common.manager.AbstractVersionedManager;
import com.net.xpay.core.model.CollectionSafePayOrderSearchModel;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.time.LocalDateTime;
import java.util.List;


@Component
public class CollectionSafePayOrderManagerImpl extends AbstractVersionedManager<CollectionSafePayOrder> implements CollectionSafePayOrderManager {

    @Autowired
    private CollectionSafePayOrderMapper mapper;


    @Autowired
    private SerialNumberService serialNumberService;


    @Override
    protected BasicMapper<Long, CollectionSafePayOrder> getMapper() {
        return mapper;
    }



    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(CollectionSafePayOrder obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));

        return super.save(obj);
    }

    @Override
    public List<CollectionSafePayOrder> search(CollectionSafePayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("create_time", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(CollectionSafePayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public List<CollectionSafePayOrder> findByIds(List<Long> ids) {
        Preconditions.checkArgument(CollectionUtils.isNotEmpty(ids));
        return nonEmptyList(mapper.findByIds(ids));
    }

    @Override
    public int updateStatusByIds(String status, List<Long> ids) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(status) && CollectionUtils.isNotEmpty(ids));
        return mapper.updateStatusByIds(status, ids);
    }

    @Override
    public int batchUpdate(List<CollectionSafePayOrder> orders) {
        if (CollectionUtils.isEmpty(orders)) {
            return 0;
        }
        for (CollectionSafePayOrder record : orders) {
            LocalDateTime now = LocalDateTime.now();
            record.setModifyTime(now);
            record.setVersion(record.getVersion() + 1L);
        }

        return mapper.batchUpdate(orders);
    }

    @Override
    public CollectionSafePayOrder findById(Long userId, Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        CollectionSafePayOrder condition = new CollectionSafePayOrder();
        condition.setUserId(userId);
        condition.setId(id);
        return getByCondition(condition);
    }


}
